package cn.myapps.demospringsecurity.controller;

import cn.myapps.demospringsecurity.entity.User;
import cn.myapps.demospringsecurity.result.Result;
import cn.myapps.demospringsecurity.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @GetMapping("/User")
    public User getUser() {
        return userService.getById("admin");
    }

    @PostMapping("/login")
    public Result<User> login(@RequestBody User user) {
        String jwt = userService.login(user);
        if (StringUtils.hasLength(jwt)){
            return Result.success("登陆成功",user);
        }
        return (Result<User>) Result.failed();
    }

}
